//
//  XMLExercisesParser.m
//  LanguagesTeach
//
//  Created by Niel Santos on 2/9/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "XMLExercisesParser.h"
#import "Section.h"
#import "Exercise.h"


@implementation XMLExercisesParser

@synthesize exercises;

- (id)init{
	exercises = [[Exercises alloc] init];
	exercises.sections = [[NSMutableArray alloc] init];
	return [super init];
}

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
	attributes:(NSDictionary *)attributeDict{
	
	Section *newSection;
	Section *lastSection;
	Exercise *exercise;
	
	if([elementName isEqualToString:@"section"]) {
		newSection = [[Section alloc] init];
		newSection.prefix = [attributeDict objectForKey:@"prefix"];
		newSection.exercisesLst  = [[NSMutableArray alloc] init];
		[exercises.sections addObject: newSection];
		[newSection release];
	}else if([elementName isEqualToString:@"exercise"]) {
		lastSection = [exercises.sections lastObject];
		exercise = [[Exercise alloc] init];
		exercise.title = [attributeDict objectForKey:@"title"];
		exercise.prefix = [attributeDict objectForKey:@"prefix"];
		exercise._class = [attributeDict objectForKey:@"class"];
		[lastSection.exercisesLst addObject:exercise];
		[exercise release];
	}
}


- (void)dealloc {
	//TODO é necessario limpar isto aqui em condiçoes
	[exercises release];
    [super dealloc];
}

@end